package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.JyjcImportLbys;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.JyjcUserNumMapper;
import com.ruoyi.system.domain.JyjcUserNum;
import com.ruoyi.system.service.IJyjcUserNumService;

/**
 * 各部所人员数Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-25
 */
@Service
public class JyjcUserNumServiceImpl implements IJyjcUserNumService 
{
    private static final Logger log = LoggerFactory.getLogger(JyjcUserNumServiceImpl.class);

    @Autowired
    private JyjcUserNumMapper jyjcUserNumMapper;

    /**
     * 查询各部所人员数
     * 
     * @param id 各部所人员数主键
     * @return 各部所人员数
     */
    @Override
    public JyjcUserNum selectJyjcUserNumById(Long id)
    {
        return jyjcUserNumMapper.selectJyjcUserNumById(id);
    }

    /**
     * 查询各部所人员数列表
     * 
     * @param jyjcUserNum 各部所人员数
     * @return 各部所人员数
     */
    @Override
    public List<JyjcUserNum> selectJyjcUserNumList(JyjcUserNum jyjcUserNum)
    {
        return jyjcUserNumMapper.selectJyjcUserNumList(jyjcUserNum);
    }

    /**
     * 新增各部所人员数
     * 
     * @param jyjcUserNum 各部所人员数
     * @return 结果
     */
    @Override
    public int insertJyjcUserNum(JyjcUserNum jyjcUserNum)
    {
        return jyjcUserNumMapper.insertJyjcUserNum(jyjcUserNum);
    }

    /**
     * 修改各部所人员数
     * 
     * @param jyjcUserNum 各部所人员数
     * @return 结果
     */
    @Override
    public int updateJyjcUserNum(JyjcUserNum jyjcUserNum)
    {
        return jyjcUserNumMapper.updateJyjcUserNum(jyjcUserNum);
    }

    /**
     * 批量删除各部所人员数
     * 
     * @param ids 需要删除的各部所人员数主键
     * @return 结果
     */
    @Override
    public int deleteJyjcUserNumByIds(Long[] ids)
    {
        return jyjcUserNumMapper.deleteJyjcUserNumByIds(ids);
    }

    /**
     * 删除各部所人员数信息
     * 
     * @param id 各部所人员数主键
     * @return 结果
     */
    @Override
    public int deleteJyjcUserNumById(Long id)
    {
        return jyjcUserNumMapper.deleteJyjcUserNumById(id);
    }

    @Override
    public String importJyjcUserNum(List<JyjcUserNum> jyjcUserNumList, String yearyf)
    {
        String [] sj=yearyf.split("-");

        if (StringUtils.isNull(jyjcUserNumList) || jyjcUserNumList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        jyjcUserNumMapper.deletejyjcUserNumByYearYf(sj[0]+"-"+sj[1]);
        for (JyjcUserNum jyjcUserNum : jyjcUserNumList)
        {
            try
            {
                jyjcUserNum.setYearyf(sj[0]+"-"+sj[1]);
                jyjcUserNumMapper.insertJyjcUserNum(jyjcUserNum);
                successNum++;
                successMsg.append("<br/>" + successNum + "、 " + sj[0]+"年"+sj[1]+"月"+jyjcUserNum.getDeptname()  + " 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + sj[0]+"年"+sj[1]+"月"+jyjcUserNum.getDeptname() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
